<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\common\command;


use app\common\model\buy\EgoldCountLog as EgoldCountLogModel;
use app\common\model\buy\EgoldLog as EgoldLogModel;
use app\common\model\user\User as UserModel;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Log;


class ExpiredEgold extends Command
{
    protected function configure()
    {
        $this->setName('expired_egold')
            ->setDescription('赠币过期作废');
    }


    protected function execute(Input $input, Output $output)
    {
        try {
            $time = strtotime(date('Y-m-d',time()));
            for ($i = 0; $i < 50; $i++) {
                $suffix = '_'.$i;
                $list = EgoldCountLogModel::suffix($suffix)->where("expire_time","<",$time)->where("delete_time","=",0)->group('user_id')->field('*,sum(egold) as sumegold')->order('id asc')->select();
                foreach ($list as $k=>$v){
                EgoldCountLogModel::suffix($suffix)->where('user_id',$v['user_id'])->where("expire_time","<",$time)->update(['delete_time'=>$time]);
                    $data = [
                        'user_id'=>$v['user_id'],
                        'type'=>8,
                        'title'=>'赠送书币过期',
                        'egold'=>$v['sumegold'],
                        'create_time'=>$time,
                        'update_time'=>$time,
                    ];
                    EgoldLogModel::EgoldLogQuery($v['user_id'])->create($data);
                }
            }

            $output->writeln('expired_egold');
            return true;
        } catch (\Exception $e) {
            Log::write('赠币过期处理失败,失败原因:' . $e->getMessage());
            return false;
        }
    }

}